package com.jianggy.proxy.jdk2;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

/**
 * @author jianggy
 * @version 1.0
 * @time 2019/11/16 19:18
 * @see
 **/
public class App {
    public static void main(String[] args) {
        DynamicSubject subject = (DynamicSubject) Proxy.newProxyInstance(App.class
                .getClassLoader(), new Class[]{DynamicSubject.class}, new SubjectInvocationHandler(new RealDynamicSubject()));
        // subject.method1();
        // subject.method2();
        //将生成的代理类字节码输出到文件，然后我们就可以使用反编译工具看到了。。。。
        byte[] bytes = ProxyGenerator.generateProxyClass("DynamicSubjectProxy", new Class[]{DynamicSubject.class});
        try {
            FileOutputStream outputStream = new FileOutputStream(
                    "C:\\Users\\jianggy\\Desktop\\DynamicSubjectProxy.class");
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
